package com.ebspp.client.watcher;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

import com.ebspp.global.EBSPPTimer;
import com.ebspp.global.General;
import com.ebspp.thrift.DataMsg;
import com.ebspp.thrift.Identity;
import com.ebspp.thrift.ResourceType;

public class WatchBuffer 
{
	int maxBufSize = 0;
	int bufID;
	Identity wID;
	ResourceType resType;
	LinkedBlockingQueue<DataMsg> queue;
	
	public WatchBuffer(int maxBufSize, ResourceType resType, Identity wID)
	{
		this.bufID = General.getBufID();
		this.maxBufSize = maxBufSize;
		this.wID = wID;
		this.resType = resType;
		queue = new LinkedBlockingQueue<DataMsg>();
	}
	
	public int getBufID()
	{
		return bufID;
	}
	
	public void write(List<DataMsg> data)
	{
		while(data.size() + queue.size() > maxBufSize)
		{
			try {
				Thread.sleep(EBSPPTimer.watchBufWaitT);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		queue.addAll(data);
	}
	
	public List<DataMsg> read(int size)
	{
		if(size < 0)
			size = Integer.MAX_VALUE;
		
		List<DataMsg> res = new ArrayList<DataMsg>();
		queue.drainTo(res, size);
		return res;
	}
	
	
	public void close()
	{
		//todo
	}
}
